package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.db.Authentication;
import com.apiit.webserviceclient.db.Authentication_DAO;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;
import com.apiit.webserviceclient.utility.GlobalData;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ApplyLeaveLogin extends Activity implements OnClickListener {
	
	private Authentication_DAO datasource1;
	EditText username;
	EditText pass;
	Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_apply_leave_login);
		
		username = (EditText) findViewById(R.id.editTxtPagolusername);
		pass = (EditText) findViewById(R.id.EditTxtPagolpassword);
		button = (Button) findViewById(R.id.btnPagolLogin);
		button.setOnClickListener(this);

		datasource1 = new Authentication_DAO(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.apply_leave_login, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == button) {
			datasource1.open();


			BPEL_Process bpelProcess3 = new BPEL_Process();
			BPEL_Request request3 = new BPEL_Request();
			request3.setUrl("http://webspace1.apiit.lk/eHRM-Web/LoginPage.aspx ");

			Map<String, String> xpathMap3 = new HashMap<String, String>();
			xpathMap3.put("test","/html[1]/body[1]/table[1]/tr[1]/td[1]/table[1]/tr[2]/td[2]/a[6]---");
			request3.setXpathMap(xpathMap3);
			Map<String, String> parametersMap3 = new HashMap<String, String>();
			parametersMap3.put("__VIEWSTATE", "%2FwEPDwULLTE4MTc4MjA1NzFkZA%3D%3D");
			parametersMap3.put("__EVENTVALIDATION", "%2FwEWBgK8rOK2AgKPpuq2CALyveCRDwKPyPGACgLfndyKBAL5i%2BPNBg%3D%3D");
			parametersMap3.put("userName",username.getText().toString());
			parametersMap3.put("password",pass.getText().toString() );
			parametersMap3.put("login", "Login");
			parametersMap3.put("password","statistics" );
			parametersMap3.put("login", "Login");
			parametersMap3.put("FooterBarUI1%3AscreenTopHidden","0" );
			parametersMap3.put("FooterBarUI1%3AscreenLeftHidden", "0");
			request3.setParametersMap(parametersMap3);
			request3.setType("POST");
			BPEL_Response response3 = null;
			try {
				response3 = bpelProcess3.doRequest(request3);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}


			String testResult = response3.getResultsMap().get("test");

			Toast.makeText(getApplicationContext(), testResult,
					Toast.LENGTH_LONG).show();

			if (response3.getStatus().equalsIgnoreCase(GlobalData.message_success)) {
				if (datasource1.getAction(GlobalData.autherntication_id_applyLeave) == null) {
					Authentication authenticaton = new Authentication();
					authenticaton.setAuthentication_id(GlobalData.autherntication_id_applyLeave);
					authenticaton.setauthentication_name(GlobalData.authentication_name_applyLeave);
					authenticaton.setuser_name(username.getText().toString());
					authenticaton.setpassword(pass.getText().toString());
					datasource1.createAction(authenticaton);
				}
				else
				{
					Authentication authenticaton = new Authentication();
					authenticaton.setuser_name(username.getText().toString());
					authenticaton.setpassword(pass.getText().toString());
					datasource1.updateAuthenticationDetails(GlobalData.autherntication_id_applyLeave, GlobalData.authentication_name_applyLeave, username.getText().toString(), pass.getText().toString());
					
				}
				

			}

			datasource1.close();
			Intent i = new Intent(this, ApplyLeave.class);
			i.putExtra("title", testResult);
			startActivity(i);
		}
	}

}
